package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.ListService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author: 李旭
 * @create: 2021-07-13 14:44
 **/
@RestController
@RequestMapping("/api/list")
public class ListApiController {


   /* @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;*/
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;//ES公司原装版本的客户端进行了封装
    //封装之后比原装版本更加方便 使用 Api更加简单

    @Autowired
    private RestHighLevelClient restHighLevelClient;//性能角度是最高   ES公司原装版本的客户端
/*    @Autowired
    private TransportClient transportClient;*/

    @Autowired
    private ListService listService;

    //根据业务创建数据结构
    @GetMapping("/createData")
    public Result createData() {
        //Spring公司
        elasticsearchRestTemplate.createIndex(Goods.class);//创建索引库
        elasticsearchRestTemplate.putMapping(Goods.class);//Mapping 映射  构建数据结构

        //ES公司原装版本的客户端  创建索引库
/*        IndicesClient indices = restHighLevelClient.indices();
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("goods");
        try {
            indices.create(createIndexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        return Result.ok();
    }

    //上架  保存索引
    @PostMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        listService.onSale(skuId);
        return Result.ok();
    }
    //下架  删除索引
    @DeleteMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        listService.cancelSale(skuId);
        return Result.ok();
    }
    //更新热度
    @PutMapping("/inner/hotScore/{skuId}")
    public void hotScore(@PathVariable Long skuId){
        listService.hotScore(skuId);
    }

    //开始搜索
    @PostMapping
    public SearchResponseVo list(@RequestBody SearchParam searchParam){
        return listService.list(searchParam);

    }

}
